package st.business.tepth.business.user.busnumber.workholder.dialog;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.R;
import st.business.tepth.business.bean.DiagnoseBean;
import st.business.tepth.business.bean.PartBean;
import st.business.tepth.business.bean.ProgramBean;
import st.business.tepth.business.bean.RepairTaskBean;
import st.business.tepth.business.event.DiagnoseSelectEvent;
import st.business.tepth.config.ItemFields;
import st.shadow.library.ui.indicator.magicindicator.MagicIndicator;
import st.shadow.library.ui.indicator.magicindicator.ViewPagerHelper;
import st.shadow.library.ui.indicator.magicindicator.buildins.UIUtil;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.CommonNavigator;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.indicators.WrapPagerStrokeIndicator;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2019/2/21
 */
public class ViewContactProgram extends LinearLayout {
    private ViewPager viewPager;

    public ViewContactProgram(Context context) {
        super(context);
    }

    public ViewContactProgram(Context context, RepairTaskBean taskBean) {
        super(context);
        View content = LayoutInflater.from(context).inflate(R.layout.view_contact_program_trouble,
                null);
        MagicIndicator magicIndicator = content.findViewById(R.id.indicator);
        final ArrayList<RepairTaskBean.FaultPhenomenonListBean> faultPhenomenonList
                = taskBean.getFaultPhenomenonList();
        final List<RecyclerView> recyclerViews = initRecyclerView(faultPhenomenonList);
        viewPager = content.findViewById(R.id.view_pager);
        viewPager.setAdapter(new PagerAdapter() {

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull final ViewGroup container, int position) {
                container.addView(recyclerViews.get(position), ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
                return recyclerViews.get(position);
            }

            @Override
            public int getCount() {
                return faultPhenomenonList.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
                return view == o;
            }
        });
        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setLeftPadding(0);
        commonNavigator.setAdapter(new DiagnoseAdapter(faultPhenomenonList));

        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
        addView(content);
    }

    private List<RecyclerView> initRecyclerView(ArrayList<RepairTaskBean.FaultPhenomenonListBean> faultPhenomenonList) {
        List<RecyclerView> recyclerViews = new ArrayList<>();
        if (faultPhenomenonList != null) {
            for (RepairTaskBean.FaultPhenomenonListBean bean : faultPhenomenonList) {
                List<MultipleItemEntity> multipleItemEntities = new ArrayList<>();
                ArrayList<DiagnoseBean> diagnoseBeans = bean.getFaultDiagnosticTemporaryList();
                if (diagnoseBeans != null) {
                    for (DiagnoseBean diagnoseBean : diagnoseBeans) {
                        List<ProgramBean> programBeanList = diagnoseBean.getPlanList();
                        if (programBeanList != null) {
                            for (ProgramBean programBean : programBeanList) {
                                multipleItemEntities.add(MultipleItemEntity
                                        .builder().setField("isCheck", false)
                                        .setField("diagnoseName", diagnoseBean.getName())
                                        .setField(ItemFields.ITEM_OBJ, programBean)
                                        .build());
                            }
                        }
                    }
                }
                RecyclerView recyclerView = new RecyclerView(getContext());
                recyclerView.setLayoutManager(
                        new LinearLayoutManager(getContext()));
                recyclerView.setAdapter(new BaseQuickAdapter<MultipleItemEntity, BaseViewHolder>
                        (R.layout.item_contact_program, multipleItemEntities) {
                    @Override
                    protected void convert(final BaseViewHolder helper, final MultipleItemEntity item) {
                        final ProgramBean programBean = item.getField(ItemFields.ITEM_OBJ);
                        helper.setText(R.id.tv_diagnose_name,
                                (CharSequence) item.getField("diagnoseName"));
                        helper.setText(R.id.tv_diagnose_operation,
                                convertProgramToString(programBean));
                        if (item.getField("isCheck")) {
                            helper.setBackgroundRes(R.id.layout,
                                    R.drawable.shape_btn_solid_trans_with_stroke_yellow);
                            helper.setTextColor(R.id.tv_diagnose_name,
                                    Color.parseColor("#f1b647"));
                            helper.setTextColor(R.id.tv_diagnose_operation,
                                    Color.parseColor("#f1b647"));
                        } else {
                            helper.setBackgroundRes(R.id.layout,
                                    R.drawable.shape_btn_solid_trans_with_stroke_gray);
                            helper.setTextColor(R.id.tv_diagnose_name,
                                    Color.parseColor("#333333"));
                            helper.setTextColor(R.id.tv_diagnose_operation,
                                    Color.parseColor("#333333"));
                        }
                        helper.getView(R.id.layout).setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (item.getField("isCheck")) {
                                    item.setField("isCheck", false);
                                    helper.setBackgroundRes(R.id.layout,
                                            R.drawable.shape_btn_solid_trans_with_stroke_gray);
                                    helper.setTextColor(R.id.tv_diagnose_name,
                                            Color.parseColor("#333333"));
                                    helper.setTextColor(R.id.tv_diagnose_operation,
                                            Color.parseColor("#333333"));
                                    EventBus.getDefault().post(new DiagnoseSelectEvent(programBean, false));
                                } else {
                                    item.setField("isCheck", true);
                                    helper.setBackgroundRes(R.id.layout,
                                            R.drawable.shape_btn_solid_trans_with_stroke_yellow);
                                    helper.setTextColor(R.id.tv_diagnose_name,
                                            Color.parseColor("#f1b647"));
                                    helper.setTextColor(R.id.tv_diagnose_operation,
                                            Color.parseColor("#f1b647"));
                                    EventBus.getDefault().post(new DiagnoseSelectEvent(programBean, true));
                                }
                            }
                        });
                    }
                });
                recyclerViews.add(recyclerView);
            }
        }
        return recyclerViews;
    }

    private String convertProgramToString(ProgramBean programBean) {
        PartBean partBean = programBean.getPart();
        String action = programBean.getAction_name();
        return String.format("%s%s", action, partBean == null ? ""
                : partBean.getMaterial_name());
    }

    private class DiagnoseAdapter extends CommonNavigatorAdapter {
        ArrayList<RepairTaskBean.FaultPhenomenonListBean>
                mFaultPhenomenonListBeans;

        public DiagnoseAdapter(ArrayList<RepairTaskBean.FaultPhenomenonListBean>
                                       faultPhenomenonList) {
            mFaultPhenomenonListBeans = faultPhenomenonList;
        }

        @Override
        public int getCount() {
            return mFaultPhenomenonListBeans.size();
        }

        @Override
        public IPagerTitleView getTitleView(Context context, final int index) {
            SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
            simplePagerTitleView.setGravity(Gravity.CENTER_VERTICAL);
            simplePagerTitleView.setText(mFaultPhenomenonListBeans.get(index)
                    .getName());
            simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
            simplePagerTitleView.setNormalColor(Color.parseColor("#333333"));
            simplePagerTitleView.setSelectedColor(Color.parseColor("#dd9328"));
            simplePagerTitleView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewPager.setCurrentItem(index);
                }
            });
            return simplePagerTitleView;
        }

        @Override
        public IPagerIndicator getIndicator(Context context) {
            WrapPagerStrokeIndicator indicator = new WrapPagerStrokeIndicator(context);
            indicator.setFillColor(Color.parseColor("#dd9328"));
            indicator.setVerticalPadding(UIUtil.dip2px(context, 2));
            indicator.setRoundRadius(2);
            return indicator;
        }
    }

}
